//
//  PaperOptionView.m
//  ZYOnlineSchool 选项View
//
//  Created by 毕武侠 on 2017/3/7.
//  Copyright © 2017年 bwx. All rights reserved.
//

#import "PaperOptionView.h"
#import "PaperAnswerModel.h"
@interface PaperOptionView ()<UITextViewDelegate>

@property (nonatomic, assign) NSUInteger maxIndex;

@end

@implementation PaperOptionView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
//        self.backgroundColor = [UIColor redColor];
        [self setupConfigure];
    }
    return self;
}

- (void)setupConfigure {
    self.maxIndex = 0;
}

- (void)setOptionType:(NSInteger)optionType {
    _optionType = optionType;
}

- (void)setOptionDatas:(NSMutableArray *)optionDatas {
    _optionDatas = optionDatas;
    
    UITextView *everView = [self viewWithTag:999];
    if(everView){
        [everView removeFromSuperview];
    }
    
    __block CGFloat top = 0;
    [optionDatas enumerateObjectsUsingBlock:^(PaperAnswerOptionModel *model, NSUInteger idx, BOOL * _Nonnull stop) {
        UILabel *titleLab = [self getOptionTitleWithIndex:idx];
        titleLab.text = model.title;
        titleLab.top = top;
        if (self.optionType == 0 || self.optionType == 2) {
            titleLab.layer.cornerRadius = 14.0f;
        } else {
            titleLab.layer.cornerRadius = 6.0f;
        }
        
        titleLab.hidden = NO;
        
        YYLabel *contentLab = [self getOptionContentWithIndex:idx];
        contentLab.height = model.layout.textBoundingSize.height < 40 ? 40 : model.layout.textBoundingSize.height;
        contentLab.textLayout = model.layout;
        contentLab.top = top;
        contentLab.hidden = NO;
        titleLab.centerY = contentLab.centerY;
        
        UIButton *optionBtn = [self getOptionButtonWithIndex:idx];
        optionBtn.height = contentLab.height;
        optionBtn.top = top;
        optionBtn.hidden = NO;
        
        [self setOptionType:model.type atIndex:idx];
        
#if TGT_XBGK
        top += contentLab.height + 25.0f;
#else
        top += contentLab.height + 8.0f;
#endif
        
        // 2017/7/17后加，解决文字显示不全问题
        YYTextContainer *container = [YYTextContainer new];
        container.size = contentLab.frame.size;
        container.maximumNumberOfRows = 0;
        YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:model.layout.text];
        contentLab.textLayout = layout;
        contentLab.attributedText = model.layout.text;
        
    }];
    
    // 隐藏多余的选项
    if (optionDatas.count < self.maxIndex) {
        for (NSInteger i = optionDatas.count; i < self.maxIndex; i++) {
            UILabel *titleLab = [self getOptionTitleWithIndex:i];
            titleLab.hidden = YES;
            
            YYLabel *contentLab = [self getOptionContentWithIndex:i];
            contentLab.hidden = YES;
            
            UIButton *optionBtn = [self getOptionButtonWithIndex:i];
            optionBtn.hidden = YES;
        }
    }
    
    if (optionDatas.count > self.maxIndex) {
        self.maxIndex = optionDatas.count;
    }
    
    self.height = top;

    
}

- (void)setAnswerModel:(PaperAnswerModel *)answerModel{
    UITextView *everView = [self viewWithTag:999];
    if(everView){
        [everView removeFromSuperview];
    }
    if (self.optionType == 2) {//问答题
        [self setOptionDatas:@[].mutableCopy];
        UITextView *textView = [self getAnswerTextViewWithIndex:999];
        if (answerModel.user_content) {
            textView.text  = answerModel.user_content;
        }
        self.height = 100;
    }
}

- (void)setQA_content:(NSString *)content{
    UITextView *textView = [self getAnswerTextViewWithIndex:999];
    textView.text = content;
}


- (void)setOptionType:(PaperOptionType)type atIndex:(NSUInteger)index {
    UILabel *titleLab = [self getOptionTitleWithIndex:index];
    PaperAnswerOptionModel *model = self.optionDatas[index];
    model.type = type;
    
    if (type == PaperOptionTypeNormal || PaperOptionTypeNone) {
        titleLab.textColor = MainThemeColor;
        titleLab.backgroundColor = [UIColor whiteColor];
        titleLab.layer.borderColor = MainThemeColor.CGColor;
    } else if (type == PaperOptionTypeRight || type == PaperOptionTypeSelect) {
        titleLab.textColor = [UIColor whiteColor];
        titleLab.backgroundColor = MainThemeColor;
        titleLab.layer.borderColor = MainThemeColor.CGColor;
    } else if (type == PaperOptionTypeWrong) {
        titleLab.textColor = [UIColor whiteColor];
        titleLab.backgroundColor = [UIColor redColor];
        titleLab.layer.borderColor = [UIColor redColor].CGColor;
    }
}


#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView
{
    //   - (void)sendQA_content:(NSString *)content;

    if (textView.markedTextRange == nil) {
//        NSLog(@"text:%@", textView.text);
        if ([self.delegate respondsToSelector:@selector(sendQA_content:)]) {
            [self.delegate sendQA_content:textView.text];
        }
        
    }
}

#pragma mark - btn action

- (void)OptionAction:(UIButton *)btn {
    NSUInteger index = btn.tag / 10 - 1;
    PaperAnswerOptionModel *model = self.optionDatas[index];
    
    if (model.type != PaperOptionTypeNone && [self.delegate respondsToSelector:@selector(PaperOptionView:atIndex:)]) {
        [self.delegate PaperOptionView:self atIndex:index];
    }
}

#pragma mark - get

- (UILabel *)getOptionTitleWithIndex:(NSUInteger)index{
    NSUInteger tag = (index + 1) * 10;
    UILabel *label = [self viewWithTag:tag];
    if (label == nil) {
        label = [[UILabel alloc] initWithFrame:CGRectMake(12, 0, 28, 28)];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = MainThemeColor;
        ViewBorderRadius(label, 14, 0.8f, MainThemeColor);
        label.tag = tag;
        [self addSubview:label];
    }
    return label;
}

- (YYLabel *)getOptionContentWithIndex:(NSUInteger)index {
    NSUInteger tag = (index + 1) * 10 + 1;
    YYLabel *label = [self viewWithTag:tag];
    if (label == nil) {
        label = [[YYLabel alloc] initWithFrame:CGRectMake(50, 0, self.width - 58, 12)];
        label.textVerticalAlignment = YYTextVerticalAlignmentCenter;
        label.displaysAsynchronously = YES;
        label.ignoreCommonProperties = YES;
        label.fadeOnAsynchronouslyDisplay = NO;
        label.fadeOnHighlight = NO;
        label.lineBreakMode = NSLineBreakByCharWrapping;
        label.numberOfLines = 0;
        label.tag = tag;
        [self addSubview:label];
    }
    return label;
}

- (UIButton *)getOptionButtonWithIndex:(NSUInteger)index {
    NSUInteger tag = (index + 1) * 10 + 2;
    UIButton *btn = [self viewWithTag:tag];
    if (btn == nil) {
        btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 28)];
        [btn addTarget:self action:@selector(OptionAction:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = tag;
        [self addSubview:btn];
    }
    return btn;
}


- (UITextView *)getAnswerTextViewWithIndex:(NSUInteger)index{
    NSUInteger tag = index;
    UITextView *textView = [self viewWithTag:tag];
    if (textView == nil) {
        textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 0, screenWidth - 20, 80)];
        textView.delegate = self;
        textView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        textView.tag = tag;
        [self addSubview:textView];
    }
    return textView;
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
